package pers.cz.codegen.describe;

import pers.cz.codegen.template.CommentTemplate;
import pers.cz.codegen.enums.AnnotationEnum;
import pers.cz.codegen.enums.CodeStyleConfigItem;
import pers.cz.jefconfig.config.JefConfiguration;

import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;

/**
 * @program: jef-code-generator
 * @description: 抽象java描述
 * @author: Cheng Zhi
 * @create: 2022-08-07 13:58
 **/
public abstract class AbstractJavaElement implements JavaElement{

    public static final String SEMICOLON = ";";
    // 换行
    public static final String LINE_SYMBOL = "\r\n";
    // 编程规范，左大括号后要换行;
    public static final String START_SYMBOL = "{\r\n";

    public static final String END_SYMBOL = "}";
    // 空格
    public static final String SPACE_SYMBOL = "\u0020";

    // 代码风格，缩进4个空格
    public static final String TAB_SYMBOL = "\u0020\u0020\u0020\u0020";

    /**
     * 描述符号
     */
    private int modifer = Modifier.PUBLIC;

    /**
     * 代码注释
     */
    protected String comment;

    /**
     * 代码内容
     */
    private List<String> contents;

    /**
     * 注解内容
     */
    public List<String> annotations = new LinkedList<>();

    private String name;

    private String describe = "";

    public String getDescribe() {
        return describe;
    }

    public void setDescribe(String describe) {
        if (describe == null) {
            return;
        }
        this.describe = describe;
    }

    public void addAnnotation(String annotation) {
        this.annotations.add(annotation);
    }

    public void addAnnotation(AnnotationEnum annotationEnum) {
        this.annotations.add(annotationEnum.value());
    }

    /**
     * 用于注解中有值的场景，例如：@Qualifier("testBean")
     * @param annotationEnum
     * @param value
     */
    public void addAnnotation(AnnotationEnum annotationEnum, String value) {
        String finalValue = "(\"" + value + "\")";
        this.addAnnotation(annotationEnum.value() + finalValue);
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    /**
     * 获取当前代码注释
     * @return
     */
    public String getComment() {
        // 注释的前面永远是4个缩进
        if (comment == null) {
            return "";
        }
        // 将模板中的占位符替换
        if (comment.contains(CommentTemplate.descPlace)) {
            comment = comment.replace(CommentTemplate.descPlace, JefConfiguration.get(CodeStyleConfigItem.CODE_STYLE_DESCRIBE, describe));
        }

        if (comment.contains(CommentTemplate.authorPlace)) {
            comment = comment.replace(CommentTemplate.authorPlace, JefConfiguration.get(CodeStyleConfigItem.CODE_STYLE_AUTHOR, "jef-code-generator"));
        }

        if (comment.contains(CommentTemplate.timePlace)) {
            comment = comment.replace(CommentTemplate.timePlace, String.valueOf(new Date()));
        }
        return comment;
    }

    public void setComment(String comment) {
        this.comment = comment;
    }

    public List<String> getContents() {
        return contents == null ? new ArrayList<>() : contents;
    }

    public void setContents(List<String> contents) {
        if (this.contents != null) {
            // 如果集合已经被创建
            this.contents.addAll(contents);
        } else {
            this.contents = contents;
        }

    }

    public void addContent(String... conten) {
        if (this.contents == null) {
            // 如果集合还未被创建
            this.contents = new ArrayList();
        }
        for (String cont : conten) {
            this.contents.add(cont);
        }
    }

    public void setContents(String content) {
        this.contents = new ArrayList<>();
        this.contents.add(content);
    }

    public String getModifer() {
        return getModifer(this.modifer);
    }

    public void setModifer(int modifer) {
        this.modifer = modifer;
    }

    /**
     * 获取描述类型
     * @param modifer
     * @return
     */
    private String getModifer(int modifer) {

        if (Modifier.isPrivate(modifer)) {
            return "private ";
        } else if (Modifier.isPublic(modifer)) {
            return "public ";
        } else if (Modifier.isProtected(modifer)) {
            return "protected ";
        }

        return "";
    }
    /**
     * 获取code
     * @return
     */
    public abstract String toCode();
}
